//
//  CHYController.m
//  udp
//
//  Created by 崔 on 16/9/11.
//  Copyright © 2016年 崔. All rights reserved.
//

#import "CHYController.h"
#define  CHYControllerInfoPreFix  @"terminal_broadcast_info:"
#define  CHYContrillerPin_code @"pin_code:"
#define  CHYContrillerIP @"ip:"
#define  CHYContrillerMac @"mac:"



@implementation CHYController

- (instancetype)initWithData:(NSData *)data{
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    
    CHYController *controller = [[CHYController alloc] init];
    
    if ((dataStr.length)) {
        
        if ([dataStr hasPrefix:CHYControllerInfoPreFix]) { //在这里解析数据
            
            //将字符串转化为数组
            
            NSArray *files = [dataStr componentsSeparatedByString:@","];
            
            for (NSString *file in files) {
                
                if ([file hasPrefix:CHYContrillerPin_code]) {
                    
                    NSString *pin_code = [file substringFromIndex:CHYContrillerPin_code.length];
                    
                    controller.pin_code = pin_code;
                }
                
                if ([file hasPrefix:CHYContrillerIP]) {
                    
                    NSString *ip = [file substringFromIndex:CHYContrillerIP.length];
                    
                    controller.ip = ip;
                }
                
                if ([file hasPrefix:CHYContrillerMac]) {
                    
                    NSString *mac = [file substringFromIndex:CHYContrillerMac.length];
                    
                    controller.mac = mac;
                }
                
            }
            
        } else{
            
            return nil;
        }
        
    } else{
        
        return nil;
    }
    
    return controller;
}

+ (instancetype)controllerWithData:(NSData *)data{
    
    CHYController *controller = [[CHYController alloc] init];
    
    return [controller initWithData:data];
    
}
@end
